﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Resources;
using System.Reflection;
using System.IO;
using System.Diagnostics;
using System.Runtime.InteropServices;


namespace InsertEmoticonPlugin
{
    public partial class frmAddEmoticon : Form
    {
        /// <summary>
        /// Constructor motherfudger!
        /// </summary>
        public frmAddEmoticon()
        {
            InitializeComponent();
            ListViewSetSpacing(listView1, 30, 30);
            LoadImagesToForm();
        }

        private void keypressed(object o, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Escape)
            {
                this.DialogResult = DialogResult.Cancel;
                this.Close();
            }
            if (e.KeyChar == (char)Keys.Enter)
            {
                button1_Click(this, null);
            }
        }

        [DllImport("user32.dll")]
        private static extern int SendMessage(IntPtr hWnd,
                                              int msg,
                                              int wParam,
                                              int lParam);

        const int LVM_FIRST = 0x1000;

        /// <summary>
        /// This "should" do some icon spacing.
        /// </summary>
        /// <param name="theView"></param>
        /// <param name="xPos"></param>
        /// <param name="yPos"></param>
        private void ListViewSetSpacing(ListView theView,
                                        int xPos,
                                        int yPos)
        {   
            int diff = (yPos * 65536) + xPos;
            SendMessage(theView.Handle, LVM_FIRST + 53, 0, diff);
        }

        /// <summary>
        /// This here loops the resources and creates items for the listview and 
        /// the imagelist for displayin'!
        /// </summary>
        private void LoadImagesToForm()
        {
            // method variables.
            Stream emotStream = null;
            Assembly asm = Assembly.GetExecutingAssembly();
            // get the names of all the resources found.
            string[] resourceNames = asm.GetManifestResourceNames();
            // write out the # of resources.
            Debug.WriteLine(string.Format("I've found {0} resources.", resourceNames.Length));

            // loop the resources looking for images.            
            foreach (string name in resourceNames)
            {
                if (name.EndsWith(".png"))
                {
                    emotStream = asm.GetManifestResourceStream(name);
                    if(emotStream != null)
                    {
                        emotImageList.Images.Add(
                                Image.FromStream(emotStream)
                        );

                        int emotename = emotImageList.Images.Count - 1;
                        listView1.Items.Add(CleanIconName(name),"", emotename);                        
                        listView1.DoubleClick += new EventHandler(button1_Click);
                        ListViewItem thing = new ListViewItem(CleanIconName(name));
                        thing.ToolTipText = "shit";
                        
                        emotStream.Close();
                        emotStream = null;                        

                    } // end if(null)                    
                }  // end if(png)
            } // end foreach
        }

        private string CleanIconName(string name)
        {
            string cleanedIconName = name.Substring(28, name.Length - 28);
            cleanedIconName = cleanedIconName.Substring(0, cleanedIconName.Length - 4);
            return cleanedIconName; // removes the first part of the string
        }

        public string text = ""; 
        
        private void button1_Click(object sender, EventArgs e)
        {
            text = string.Format("<img src=\"http://spaces.live.com/rte/emoticons/{0}.gif\" alt=\"{0}\">", listView1.SelectedItems[0].Name);
            // If statement to handle if no emoticons have been selected
            if (listView1.SelectedItems[0].Name == null)
            {
                MessageBox.Show("You haven't selected an emoticon");
            }
            else
            {                
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

    }
}